package cn.hfatec.shms.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;

public class ExcelUtil {
    private static Logger logger = LogManager.getLogger(ExcelUtil.class);

    public static String getStringRowCellValue(Row row, int cellIndex) {
        String result = "";
        try {
            if (row != null) {
                Cell cell = row.getCell(cellIndex);
                if (cell != null)
                    switch (cell.getCellType()) {
                        case 1:
                            result = StringUtils.trimToEmpty(cell.getStringCellValue());
                            break;
                        case 0:
                            cell.setCellType(1);
                            result = StringUtils.trimToEmpty(cell.getStringCellValue());
                            break;
                        case 4:
                            result = String.valueOf(cell.getBooleanCellValue());
                            break;
                        case 3:
                            result = "";
                            break;
                        case 2:
                        default:
                            result = "";
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    public static Date getRowCellValueDate(Row row, int cellIndex) {
        Date result = null;
        try {
            if (row == null) {
                return null;
            } else {
                Cell cell = row.getCell(cellIndex);
                if (cell == null) {
                    return null;
                } else {
                    result = cell.getDateCellValue();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public String getStringCellValue(HSSFCell hssfCell) {
        if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
            // 返回布尔类型的值
            return String.valueOf(hssfCell.getBooleanCellValue());
        } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
            // 返回数值类型的值
            return String.valueOf(hssfCell.getNumericCellValue());
        } else {
            // 返回字符串类型的值
            return String.valueOf(hssfCell.getStringCellValue());
        }
    }

    public String getDateCellValue(HSSFCell cell) {
        String result = "";
        try {
            int cellType = cell.getCellType();
            if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
                Date date = cell.getDateCellValue();
                result = (date.getYear() + 1900) + "-" + (date.getMonth() + 1)
                        + "-" + date.getDate();
            } else if (cellType == HSSFCell.CELL_TYPE_STRING) {
                String date = getStringCellValue(cell);
                result = date.replaceAll("[年月]", "-").replace("日", "").trim();
            } else if (cellType == HSSFCell.CELL_TYPE_BLANK) {
                result = "";
            }
        } catch (Exception e) {
            System.out.println("日期格式不正确!");
            e.printStackTrace();
        }
        return result;
    }

}